package game;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Author: liwan
 * @Description: 鱼
 * @Date: create in 2021/8/31 16:15:35
 */
public class Fish extends Thread {
    private int x;
    private int y;
    private int w;
    private int h;
    private int speedX;
    private int speedY;
    private BufferedImage image;
    private GamePanel panel;
    private List<BufferedImage> fishList = new ArrayList<>();
    private boolean catched = false;
    private List<BufferedImage> catchAnimation = new ArrayList<>();     // 被抓的鱼
    private int catchImgNum = 2;
    private int score;  // 分数

    Random rd = new Random();

    public Fish(GamePanel panel) {
        this.panel = panel;
        String fishName = randomFishName();
        this.image = ImageUtil.getImg("../img/" + fishName);
        this.w = this.image.getWidth();
        this.h = this.image.getHeight();
        this.x = rd.nextInt(GameFrame.W - this.x);
        this.y = rd.nextInt(GameFrame.H - this.y);
        this.speedX = rd.nextInt(10) + 1;
        this.speedY = rd.nextInt(10) + 1;
        //鱼的图片
        for (int i = 0; i < 10; i++) {
            String pre = fishName.substring(0, + "fish10_".length() + 1);
            fishList.add(ImageUtil.getImg("../img/" + pre + i + ".png"));
        }
        int fishType = Integer.parseInt(fishName.substring(4, 6));
        this.score = fishType;
        catchImgNum = fishType <= 7 ? 2 : 4;
        for(int i = 1; i <= catchImgNum; i++){
            String fishCatchName = "";
            //拼接名字
            if (fishType < 10) {
                fishCatchName = "fish0" + fishType + "_catch_0" + i + ".png";
            }else {
                fishCatchName = "fish" + fishType + "_catch_0" + i + ".png";
            }
            //根据图片名称找到图片
            BufferedImage image = ImageUtil.getImg("../img/" + fishCatchName);
            catchAnimation.add(image);
        }
    }

    /**
     * @Author: liwan
     * @Description: 随机鱼的种类
     * @Params:
     * @Return: {@link java.lang.String} 返回图片路径
     * @DateTime: 2021/9/1 9:47:37
     */
    public String randomFishName() {
        int cmd = rd.nextInt(100) + 1;   // [1,100]之间的数字
        if (cmd <= 95) {
            int index = rd.nextInt(9) + 1; // [1,9]之间的数
            return "fish0" + index + "_01.png";
        } else {
            int index = rd.nextInt(2) + 10; // [10,11]之间的数
            return "fish" + index + "_01.png";
        }
    }

    int temp = 0;
    /**
    * @Author: liwan
    * @Description: 鱼移动
    * @Params:
    * @Return:
    * @DateTime: 2021/9/2 9:43:55
    */
    public void move() {
        this.x -= rd.nextInt(10) + 1;
        if (this.x < -this.w) {
            this.x = GameFrame.W - 20;
            this.y = rd.nextInt(GameFrame.H - h);
        }
        temp++;
        this.image = fishList.get(temp % 10);
        panel.repaint();
    }

    @Override
    public void run() {
        while (true) {
            move();
            try {
                Thread.sleep(100);
                if (this.catched) {
                    fishOver(); // 鱼G了
                    this.x = GameFrame.W - 20;
                    this.y = rd.nextInt(GameFrame.H - h);
                    this.catched = false;
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    /**
    * @Author: liwan
    * @Description: 鱼的死亡动画
    * @Params:
    * @Return:
    * @DateTime: 2021/9/2 14:59:30
    */
    private void fishOver() {
        for (int i = 1; i <= 5; i++) {
            for (int j = 0; j < catchImgNum; j++) {
                this.image = catchAnimation.get(j); //  修改鱼的图片
                this.w = this.image.getWidth();
                this.h = this.image.getHeight();
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                panel.repaint();
            }
        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getW() {
        return w;
    }

    public void setW(int w) {
        this.w = w;
    }

    public int getH() {
        return h;
    }

    public void setH(int h) {
        this.h = h;
    }

    public int getSpeedX() {
        return speedX;
    }

    public void setSpeedX(int speedX) {
        this.speedX = speedX;
    }

    public int getSpeedY() {
        return speedY;
    }

    public void setSpeedY(int speedY) {
        this.speedY = speedY;
    }

    public BufferedImage getImage() {
        return image;
    }

    public void setImage(BufferedImage image) {
        this.image = image;
    }

    public GamePanel getPanel() {
        return panel;
    }

    public void setPanel(GamePanel panel) {
        this.panel = panel;
    }

    public boolean isCatched() {
        return catched;
    }

    public void setCatched(boolean catched) {
        this.catched = catched;
    }

    public boolean getCatched() {
        return catched;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}
